package com.example.multithread.chapter02;

import com.example.multithread.chapter01.VolatileEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.stream.IntStream;

/**
 * @ClassName ThreadPool01
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2020/4/28 7:43 PM
 * @Version 1.0
 */
public class ThreadPool01 {
	volatile int a = 0;
	static Logger logger = LoggerFactory.getLogger(ThreadPool01.class);
	public static void main(String[] args) throws InterruptedException {


//		byte[] ob = new byte[1024 * 2014];
//
//		List<byte[]> arr = new ArrayList<>();
//		while (true) {
//			arr.add(ob);
//			Thread.sleep(100);
//
//		}
		ExecutorService executor = Executors.newFixedThreadPool(2);
		IntStream.rangeClosed(1, 1000000000).forEach(i-> executor.execute(new VolatileEx()));
//		IntStream.rangeClosed(1, 100).forEach(i-> executor.submit(new VolatileEx()));
		executor.shutdown();
		while (true) {
			if (executor.isTerminated()) {
				logger.info("volatile value is {}", VolatileEx.a);
				break;
			}
		}


		Optional<BigDecimal> reduce = IntStream.rangeClosed(1, 100).boxed().map(BigDecimal::new).reduce(BigDecimal::add);

//
//
//
//		//1.8之前
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				logger.info("原始");
//			}
//		});
//
//		//1.8之后
//		new Thread(() -> logger.info("新型"));
//
//
//		//1.8之前
//		new Thread(new FutureTask<Double>(new Callable<Double>() {
//			@Override
//			public Double call() throws Exception {
//				return Math.random();
//			}
//		}));
//
//		//1.8之后
//		new Thread(new FutureTask<>(Math::random));
//
////		Thread.State.NEW
	}
}
